/**
 * 路由器相产三打哈
 */

import { createMemoryHistory, createRouter } from 'vue-router'
import login from '../views/login/index.vue'


const routes = [
    //二级路由..
    {
        path: '/login',
        name: 'login',
        component: login
    },{
        path: '/',
        name: 'home',
        component: () => import('../components/layout/Index.vue')
    }
]

/**
 * vue router 路由器实例
 */
const router = createRouter({
    history: createMemoryHistory(),
    routes,
})

/**
 * 前置守卫 AOP before 
 */
router.beforeEach((to, from, next) => {
    console.log(1,to.path)
    if (to.path === '/login') {
        next();
    }else{
        if(sessionStorage.getItem("mytoken")!= null){
            console.log(3,to.path)
            next();
        }else{
         console.log(4,to.path)
        next('/login');
     }
    }
    return true;
});

export default router